package com.espirit.eap.relationship;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.espirit.eap.sa.FieldPermission;
import com.googlecode.cswish.struts.spring.GenericService;

@Service
public class PartyRelationshipService {

	
	@Resource
	private GenericService genericService;
	public static final PartyRelationshipType PRSDEFAULT = new PartyRelationshipType();
	
	@Transactional
	public <P extends Party,C extends Party> void addRelationship(C partyChild,P partyParent,PartyRelationshipType prst,Date validFrom,Date validTo,List<FieldPermission> fieldPermissions){
		if (partyParent == null) return;
		if (partyParent.contains(partyChild,prst) != null) return ;
		
		PartyRelationship prs = new PartyRelationship();
		prs.setPartyChild(partyChild);
		prs.setPartyParent(partyParent);
		
		if (prst != null){
			prs.setPartyRelationshipType(prst);
		}
		prs.setValidFrom(validFrom);
		prs.setValidTo(validTo);
		genericService.add(prs);
	}
	
	
	@Transactional
	public <P extends Party,C extends Party> void removeRelationship(C partyChild,P partyParent,PartyRelationshipType prst){
		PartyRelationship prs = partyParent.contains(partyChild,prst);
		if (prs == null) return ;
		genericService.delete(prs);
	}
	
	
	public <P extends Party,C extends Party> void addRelationship(C partyChild,P partyParent){
		this.addRelationship(partyChild,partyParent,null,null);
	}
	
	public <P extends Party,C extends Party> void addRelationship(C partyChild,P partyParent,Date validFrom,Date validTo){
		this.addRelationship(partyChild,partyParent,null,validFrom,validTo,null);
	}
}
